package com.zklh.leetcode.top100.problem141;

import com.zklh.leetcode.tree.model.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Nelson
 * @Description 解法1
 * @create 2023-02-07 16:03
 */
public class Solution1 {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> nodeSet = new HashSet<>();
        ListNode current = head;
        while (current != null) {
            if (nodeSet.contains(current)) {
                return true;
            }
            nodeSet.add(current);
            current = current.next;
        }
        return false;
    }

    public static void main(String[] args) {
        Solution1 solution = new Solution1();

        ListNode head = new ListNode(3);
        head.next = new ListNode(2);
        head.next.next = new ListNode(0);
        head.next.next.next = new ListNode(-4);
        head.next.next.next.next = head.next;

        System.out.println(solution.hasCycle(head));
    }
}
